# -*- coding: utf-8 -*-

from fastapi import APIRouter, HTTPException, Form, Depends
from typing import Dict, Any
from ..services.chat_service import ChatService
from ..config import settings

router = APIRouter(tags=["sessions"])

@router.post("/create-session", response_model=Dict[str, Any])
def create_session(
    agent_name: str = Form(..., description="Agent名称"),
    agent_description: str = Form(..., description="Agent描述"),
    model_name: str = Form(settings.DEFAULT_MODEL, description="模型名称"),
    temperature: float = Form(settings.DEFAULT_TEMPERATURE, description="温度参数"),
    chat_service: ChatService = Depends(ChatService)
):
    """创建新会话"""
    if not agent_name or len(agent_name) < 2:
        raise HTTPException(status_code=400, detail="Agent名称至少需要2个字符")
    
    session_id = chat_service.create_session(
        agent_name=agent_name,
        agent_description=agent_description,
        model_name=model_name,
        temperature=temperature
    )
    
    return {
        "status": "success",
        "session_id": session_id,
        "message": "会话创建成功",
        "config": {
            "agent_name": agent_name,
            "model_name": model_name,
            "temperature": temperature
        }
    }

@router.post("/clear-history", response_model=Dict[str, str])
def clear_history(
    session_id: str = Form(..., description="会话ID"),
    chat_service: ChatService = Depends(ChatService)
):
    """清除会话历史，但保留知识库"""
    return chat_service.clear_history(session_id)

@router.delete("/delete-session", response_model=Dict[str, str])
def delete_session(
    session_id: str = Form(..., description="会话ID"),
    chat_service: ChatService = Depends(ChatService)
):
    """删除整个会话，包括历史记录和知识库"""
    return chat_service.delete_session(session_id)

@router.get("/session-info", response_model=Dict[str, Any])
def get_session_info(
    session_id: str,
    chat_service: ChatService = Depends(ChatService)
):
    """获取会话信息，包括配置和状态"""
    return chat_service.get_session_info(session_id)    